package com.hzlx.records.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.hzlx.BomComponentsDto;
import com.hzlx.BomsDto;
import com.hzlx.MesMaterialTransactionsDto;
import com.hzlx.productionProducts.feign.RollBack.baseDataServiceFallbackFactory;
import com.hzlx.productionProducts.feign.baseDataService;
import com.hzlx.productionProducts.feign.materialService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hzlx.records.mapper.OperationRecordsMapper;
import com.hzlx.records.domain.OperationRecords;
import com.hzlx.records.service.IOperationRecordsService;

/**
 * 工序执行记录Service业务层处理
 * 
 * @author dk
 * @date 2025-06-30
 */

@Service
public class OperationRecordsServiceImpl implements IOperationRecordsService 
{
    private static final Logger logger = LoggerFactory.getLogger(OperationRecordsServiceImpl.class);
    @Autowired
    private OperationRecordsMapper operationRecordsMapper;
    @Resource
    private baseDataService baseDataService;
    @Resource
    private materialService materialService;

    /**
     * 查询工序执行记录
     * 
     * @param recordId 工序执行记录主键
     * @return 工序执行记录
     */
    @Override
    public OperationRecords selectOperationRecordsByRecordId(Long recordId)
    {
        return operationRecordsMapper.selectOperationRecordsByRecordId(recordId);
    }

    /**
     * 查询工序执行记录列表
     * 
     * @param operationRecords 工序执行记录
     * @return 工序执行记录
     */
    @Override
    public List<OperationRecords> selectOperationRecordsList(OperationRecords operationRecords)
    {
        return operationRecordsMapper.selectOperationRecordsList(operationRecords);
    }

    /**
     * 新增工序执行记录
     * 
     * @param operationRecords 工序执行记录
     * @return 结果
     */
    @Override
    public int insertOperationRecords(OperationRecords operationRecords)
    {   //1、根据productTypeId查询 bomId  根据bomId 和operationId 查询出这道工序所需要的材料
        //（1）、获取产品id  根据产品id 查询bom表 获取bomId
        BomsDto bomsDto=new BomsDto();
        bomsDto.setProductId(Long.valueOf(operationRecords.getProductTypeId()));
        List<BomsDto> list=baseDataService.list(bomsDto);
        System.out.println("查到的bom---------------------");
        System.out.println(list);
        //（2）、根据bom_Id 和 operation_id查询mes_bom_components表,返回该道工序所需要的物料
        BomComponentsDto bomComponentsDto=new BomComponentsDto();
        bomComponentsDto.setBomId(list.get(0).getBomId());
        bomComponentsDto.setOperationId((long)(operationRecords.getOperationId()));
        List<BomComponentsDto> BomComponentsDtoS=baseDataService.BomComponentsList(bomComponentsDto);
        //2、每一道材料的quality*良品数量就等于耗材的数字、添加到物料消耗表中
        for (BomComponentsDto ComponentsDto : BomComponentsDtoS){
            BigDecimal consume = ComponentsDto.getQuantity().multiply(BigDecimal.valueOf(operationRecords.getGoodQuantity()));//消耗的材料
            MesMaterialTransactionsDto mesMaterialTransactionsDto=new MesMaterialTransactionsDto();
            mesMaterialTransactionsDto.setMaterialId(ComponentsDto.getComponentMaterialId());//设置物料id
            mesMaterialTransactionsDto.setTransactionType("工序消耗");//交易类型
            mesMaterialTransactionsDto.setQuantity(consume);//消耗材料
            mesMaterialTransactionsDto.setUnitOfMeasure(ComponentsDto.getUnitOfMeasure());
            mesMaterialTransactionsDto.setRelatedOperationRecordId(Long.valueOf(operationRecords.getOperationId()));
            mesMaterialTransactionsDto.setResponsibleUserId(operationRecords.getOperatorId());
            mesMaterialTransactionsDto.setCreatedAt(new Date());
            materialService.addByRecord(mesMaterialTransactionsDto);
        }
        return operationRecordsMapper.insertOperationRecords(operationRecords);
    }

    /**
     * 修改工序执行记录
     * 
     * @param operationRecords 工序执行记录
     * @return 结果
     */
    @Override
    public int updateOperationRecords(OperationRecords operationRecords)
    {
        return operationRecordsMapper.updateOperationRecords(operationRecords);
    }

    /**
     * 批量删除工序执行记录
     * 
     * @param recordIds 需要删除的工序执行记录主键
     * @return 结果
     */
    @Override
    public int deleteOperationRecordsByRecordIds(Long[] recordIds)
    {
        return operationRecordsMapper.deleteOperationRecordsByRecordIds(recordIds);
    }

    /**
     * 删除工序执行记录信息
     * 
     * @param recordId 工序执行记录主键
     * @return 结果
     */
    @Override
    public int deleteOperationRecordsByRecordId(Long recordId)
    {
        return operationRecordsMapper.deleteOperationRecordsByRecordId(recordId);
    }
}
